package com.yoomiito.app.ui.live;

import android.os.Bundle;


import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

//import com.netease.demo.live.activity.LiveRoomActivity;
//import com.netease.demo.live.activity.LiveRoomActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseLazyFragment;
import com.yoomiito.app.model.bean.LiveInfo;
import com.yoomiito.app.model.bean.LiveListInfo;
import com.yoomiito.app.present.home.LiveFragmentPresenter;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.widget.GridItemDecoration;
import com.yoomiito.app.widget.state.ContentType;
import com.yoomiito.app.widget.state.StatusManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class LiveFragment extends BaseLazyFragment<LiveFragmentPresenter> {

    @BindView(R.id.fm_rcy)
    RecyclerView mRecyclerView;

    @BindView(R.id.fm_refreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;

    private LiveAdapter liveAdapter;
    private List<LiveInfo> mLiveInfos = new ArrayList<>();
    private int mCurrentPage = 1;

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public View getLayoutView() {
        mStatusManager = new StatusManager.Builder(context)
                .addType(new ContentType(R.layout.fm_rcy))
                .addType(new LiveEmptyType())
                .build();
        return mStatusManager.view();
    }

    @Override
    public LiveFragmentPresenter newP() {
        return new LiveFragmentPresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initRcy();
        initRefresh();
    }

    private void initRefresh() {
        mSmartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            mCurrentPage = 1;
            getP().getLiveList(mCurrentPage);
        });
        mSmartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            mCurrentPage++;
            getP().getLiveList(mCurrentPage);
        });
        mSmartRefreshLayout.autoRefresh();
    }

    private void initRcy() {
        GridLayoutManager manager = new GridLayoutManager(context, 2);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.addItemDecoration(new GridItemDecoration(7));
        liveAdapter = new LiveAdapter(mLiveInfos);
        mRecyclerView.setAdapter(liveAdapter);
        liveAdapter.setOnItemClickListener((adapter, view, position) -> {
            LiveInfo info = mLiveInfos.get(position);
            if (info.getState() == 1) {
//                LiveRoomActivity.startAudience(context,
//                        String.valueOf(info.getId()),
//                        info.getRtmp_pull_url(),//rtmp://flv2e4bab8e.live.126.net/live/b994e71eb1974fa695cb309d7ec5ccf6
//                        true,
//                        info.getProduct_name(),
//                        String.valueOf(info.getVip_product_id()),
//                        info.getName()
//                );

            } else if (1 == info.getHas_video()) {//回放
            } else {
                YMToast.showText("暂未开播");
            }
            //播放视频
//            String url = "https://mimitao-cdn.aijiamai.com/rhxzymmt.mp4";
//            VideoPlayerActivity.startActivity(context, url);
        });
    }

    public void setListData(LiveListInfo listData) {
        if (mCurrentPage == 1) {
            mLiveInfos.clear();
            mSmartRefreshLayout.finishRefresh(true);
            if (listData == null || listData.getData() == null || listData.getData().size() == 0) {
                showEmpty();
                return;
            }

        } else {
            mSmartRefreshLayout.finishLoadMore(true);
            if (listData == null || listData.getData() == null || listData.getData().size() == 0) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
                return;
            }
        }

        mLiveInfos.addAll(listData.getData());
        liveAdapter.notifyDataSetChanged();
        mStatusManager.showContent();

    }

    public void showEmpty() {
        mStatusManager.showEmpty(LiveEmptyType.class);
    }

    public void finishRefresh() {
        if (mSmartRefreshLayout != null) {
            mSmartRefreshLayout.finishRefresh(true);
            mSmartRefreshLayout.finishLoadMore(true);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getP().getLiveList(mCurrentPage);
        }
    }
}
